package ryarithsGame;

import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.SharedMesh;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Cylinder;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;

public class PuzzleNode extends Node
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Node towerNode;
	Fence fenceNode;
	BulletNode[] checkBullets;
	boolean showSwitch, showFence, isSwitched;
	int switchType;
	Vector2f switchPos, fencePos;
	
	public PuzzleNode(String name, int type, Vector3f locSwitch, Vector3f locFence)
	{
		super(name);
		puzzleLoader(type, locSwitch, locFence);
		showSwitch = true;
		showFence = true;
		isSwitched = false;
		switchType = type;
		switchPos = new Vector2f(locSwitch.x, locSwitch.z);
		fencePos = new Vector2f(locFence.x, locFence.z);
	}
	
	private void puzzleLoader(int type, Vector3f locSwitch, Vector3f locFence)
	{
		if(type == 0)
		{
			//This cylinder will act as the four main posts at each corner
	        Cylinder postGeometry = new Cylinder("post", 20, 20, 1, 30);
	        Quaternion q = new Quaternion();
	        //rotate the cylinder to be vertical
	        q.fromAngleAxis(FastMath.PI/2, new Vector3f(1,0,0));
	        postGeometry.setLocalRotation(q);
	        postGeometry.setModelBound(new BoundingBox());
	        postGeometry.updateModelBound();
	        
	        //We will share the post 4 times (one for each post)
	        //It is *not* a good idea to add the original geometry 
	        //as the sharedmeshes will alter its local values.
	        //We then translate the posts into position. 
	        //Magic numbers are bad, but help illustrate the point.:)
	        SharedMesh post1 = new SharedMesh("post1", postGeometry);
	        post1.setLocalTranslation(locSwitch);
	        
	        towerNode = new Node("tower");
	        towerNode.attachChild(post1);
	        
	      //add the tower to the opaque queue (we don't want to be able to see through them)
	        //and we do want to see them through the forcefield.
	        towerNode.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
	        
	        //load a texture for the towers
	        TextureState ts2 = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
	        Texture t2 = TextureManager.loadTexture(PuzzleNode.class.getClassLoader()
	                  .getResource("data/textures/post1.jpg"),
	                  Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear);
	        
	        ts2.setTexture(t2);
	        
	        towerNode.setRenderState(ts2);
	        this.attachChild(towerNode);
		}
		if(type == 1)
		{
			Box box = new Box("my box", locSwitch, 2, 20, 2);
		    box.setModelBound(new BoundingSphere());
		    box.updateModelBound();
		    
		    towerNode = new Node("tower");
	        towerNode.attachChild(box);
	        

	        TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
	        ts.setEnabled(true);
	        Texture t2 = TextureManager.loadTexture(
	                PuzzleNode.class.getClassLoader().getResource(
	                "data/textures/rust1.jpg"),
	            Texture.MinificationFilter.Trilinear,
	            Texture.MagnificationFilter.Bilinear);
	        t2.setWrap(Texture.WrapMode.Repeat);
	        ts.setTexture(t2);
	        
	        towerNode.setRenderState(ts);
		    this.attachChild(towerNode);
		}
		
		fenceNode = new Fence("fenceNode", locFence.x, locFence.z, 50);
		
		this.attachChild(fenceNode);
		
	}
	
	public void updatePuzzleNode(float tpf)
	{
		if(isSwitched)
		{
			showSwitch = false;
			showFence = false;
		}
		
		
		fenceNode.updateFence(tpf);	
		if(!showSwitch)
		{
			if(this.hasChild(towerNode))
				this.detachChild(towerNode);
		}
		if(showSwitch)
		{
			if(!this.hasChild(towerNode))
				this.attachChild(towerNode);
		}
		
		if(!showFence)
		{
			if(this.hasChild(fenceNode))
				this.detachChild(fenceNode);
		}
		if(showFence)
		{
			if(!this.hasChild(fenceNode))
				this.attachChild(fenceNode);
		}
		
	}
	
	public void setShowSwitch(boolean set)
	{
		showSwitch = set;
	}
	
	public void setShowFence(boolean set)
	{
		showFence = set;
	}
	
	public int getType()
	{
		return switchType;
	}
	
	public boolean getIsSwitched()
	{
		return isSwitched;
	}
	
	public void checkSwitchCollisions(Vector2f checkVec)
	{
		//check switch
		if(switchPos.distance(checkVec) <= 5 && isSwitched == false)
		{
			isSwitched = true;
			showFence = false;
		}
	}
	
	public boolean checkFenceCollisions(Vector2f checkVec)
	{
		//check Fence
		if(fencePos.distance(checkVec) <= 40 && showFence == true)
		{
			return true;
		}
		
		return false;
	}
	public boolean checkFenceCollisions(Vector2f checkVec, boolean startInside)
	{
		//check Fence
		if(!startInside)
		{
			checkFenceCollisions(checkVec);
		}
		
		if(fencePos.distance(checkVec) <= 10 && showFence == true)
		{
			return true;
		}
		
		return false;
	}

	
	public void bulletShots(int num)
	{
		if(num == 1)
		{
			isSwitched = true;
		}
	}
	
	public Vector3f getSwitchLoc()
	{
		return new Vector3f(switchPos.x, 10, switchPos.y);
	}
	
	public Vector2f getFencePos()
	{
		return fencePos;
	}
	
	public void setFencePos(Vector2f vec)
	{
		fencePos.set(vec);
	}

	public void detachFence()
	{
		this.detachChild(fenceNode);
	}

}
